package Server.Dwc.fish;

import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import net.sf.json.JSONObject;

public class FishTableMgr {
	private static FishTableMgr _instance = null;
	public static FishTableMgr getInstance(){
		if(_instance == null)
			_instance = new FishTableMgr();
		return _instance;
	}
	
	private Long m_LastDealTime = 0L;
	private Long m_LastUpdateTime = 0L;
	
	void Init() {
		m_LastDealTime = 0L;
		m_LastUpdateTime = 0L;
	}
	
	void DismissTable(FishTable pTable) {

		if (pTable == null)
			return;

		QpGameMgr.getInstance().OnDismissRoom((IRoom) pTable);
	}
	static FishTable FindTable(int _Uid) {
		FishTable it = (FishTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}
	public static void Handle_onLeave(GameAgent agent, String[] vecArg) {
		
		Integer _Uid = agent.getUid();
		FishTable pTable = (FishTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true); 
			
			
			QpGameMgr.getInstance().Handle_OnUserRoom(agent,new JSONObject());
			
		}
		
	}
	public static void Handle_onDismissRoom(GameAgent agent, String[] vecArg) {
		Integer _Uid = agent.getUid();
		FishTable tbl = (FishTable)FindTable(_Uid);
//		if(tbl != null)
//		{
//			dthall.getInstance().NotifyRemoveUserRoom(tbl);	
//		}
		if(tbl != null && tbl.m_pIUserItem[0].dwUserID == _Uid && tbl.m_bTableStarted == false)
		{
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);	
			tbl.OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}
	}
	public static void OnRecv(GameAgent agent, JSONObject root) {
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		if (vecArg2.length == 0){
			return;
		}
		String subcmd = vecArg2[0];
		int uid = agent.getUid();
		FishTable tbl = FindTable(uid);
		if(tbl == null)
			return;
		switch (subcmd) {
		case "join":{
			tbl.Handle_onJoin(uid,vecArg2);
			return;
		}
		/*
		case "jointable":{
			tbl.Handle_onJoinTable(uid,vecArg2);
			return;
		}
		*/
		case "leave":
			Handle_onLeave(agent,vecArg2);
			return;
		case "dismissroom":
			Handle_onDismissRoom(agent,vecArg2);
			return;	
		default:
			break;
		}
	}
}
